<?php

namespace App\Http\Controllers\Api\Sign;

use App\Http\Controllers\Api\Controller;
use Illuminate\Http\Request;
use App\Http\Controllers\Api\Wechat\WxBaseController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;

class WxPayController extends Controller
{
    //
    public function getPayMsg(WxBaseController $wxBaseController)
    {
        //统一下单

        //获取users表用户的openid
//        $users = Auth::guard('api')->user();
//
//        $openid = DB::table('users')->where('id',$users->id)->value('weixin_openid');

        $notify_url = $_SERVER['SERVER_NAME'].'/api/Sign/notify';

        $out_trade_no = date('Ymdhis',time()).bin2hex(random_bytes(3));//订单号


       $arr = $wxBaseController->unifiedorder($out_trade_no,1,$notify_url,'o6O0Pw53ZvY9h97f4Y7xgFBOS7wI','testPay','' );//统一下单

        $json_msg = $wxBaseController->getJsParams($arr['prepay_id']);

//        return $this->success($json_msg);

        return response()->json($json_msg);
    }

    public function getNotifyMsg(Request $request)
    {


        $a = '<xml><appid><![CDATA[wx0a610af85b411717]]></appid>
<bank_type><![CDATA[OTHERS]]></bank_type>
<cash_fee><![CDATA[1]]></cash_fee>
<fee_type><![CDATA[CNY]]></fee_type>
<is_subscribe><![CDATA[Y]]></is_subscribe>
<mch_id><![CDATA[1562901231]]></mch_id>
<nonce_str><![CDATA[3a213834c1043d4c90ac208a204b99fe]]></nonce_str>
<openid><![CDATA[o6O0Pw53ZvY9h97f4Y7xgFBOS7wI]]></openid>
<out_trade_no><![CDATA[202006180758029f0746]]></out_trade_no>
<result_code><![CDATA[SUCCESS]]></result_code>
<return_code><![CDATA[SUCCESS]]></return_code>
<Sign><![CDATA[3BAADBD7A5E6A79B2D4478FD7EFABD4B]]></Sign>
<time_end><![CDATA[20200618155808]]></time_end>
<total_fee>1</total_fee>
<trade_type><![CDATA[JSAPI]]></trade_type>
<transaction_id><![CDATA[4200000615202006181995514321]]></transaction_id>
</xml>';

        libxml_disable_entity_loader(true);
        $arr = json_decode(json_encode(simplexml_load_string($a, 'SimpleXMLElement', LIBXML_NOCDATA)), true);

return $arr;
//        dd($arr);

//        if($request->all()){
//            $data = $request->all();
//            Log::info('this is '.$data);
//        }else{
//            Log::info('this is  error');
//        }

    }
}
